// 时间YYYY-MM-DD
export const moment = (date: Date) => {
  const d = new Date(date)
  const year = d.getFullYear()
  let month: string | number = d.getMonth() + 1
  let day: string | number = d.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  const times = year + '-' + month + '-' + day
  return times
}

//详细时间
export const momentl = (date: string | number | Date) => {
  let time
  const d = new Date(date)
  const n = new Date()
  //获取时间戳
  const dd = d.getTime()
  let h: number | string = d.getHours()
  let m: number | string = d.getMinutes()
  const Y: number | string = d.getFullYear()
  let M: number | string = d.getMonth() + 1
  let D: number | string = d.getDate()
  //现在时间
  const nn = n.getTime()
  // var hh = n.getHours();
  // var mm = n.getMinutes();
  const YY: number | string = n.getFullYear()
  const MM: number | string = n.getMonth() + 1
  const DD: number | string = n.getDate()

  if (nn - dd < 120 * 1000) {
    time = '刚刚'
    return time
  } else if (120 * 1000 < nn - dd && nn - dd <= 60 * 60 * 1000) {
    time = Math.ceil((nn - dd) / 60 / 1000) + '分钟前'
    return time
  } else if (60 * 60 * 1000 < nn - dd && D == DD && M == MM && Y == YY) {
    if (m < 10) {
      m = '0' + m
    }
    time = h + ':' + m
    return time
  } else if (Y == YY) {
    if (M < 10) {
      M = '0' + M
    }
    if (D < 10) {
      D = '0' + D
    }
    if (h < 10) {
      h = '0' + h
    }
    if (m < 10) {
      m = '0' + m
    }
    time = M + '/' + D + ' ' + h + ':' + m
    return time
  } else {
    if (M < 10) {
      M = '0' + M
    }
    if (D < 10) {
      D = '0' + D
    }
    if (h < 10) {
      h = '0' + h
    }
    if (m < 10) {
      m = '0' + m
    }
    time = Y + '/' + M + '/' + D + ' ' + h + ':' + m
    return time
  }
}

//时间hh.mm.ss
export const time = (date: string | number | Date) => {
  const d = new Date(date)
  let h: number | string = d.getHours()
  let m: number | string = d.getMinutes()
  let s: number | string = d.getSeconds()
  if (h < 10) {
    h = '0' + h
  }
  if (m < 10) {
    m = '0' + m
  }
  if (s < 10) {
    s = '0' + s
  }
  const moment = h + ':' + m + ':' + s
  return moment
}
